import urllib
import urllib2
import sys

# username = 'asfura2@gmail.com'
# password = 'chupalobien'
username = sys.argv[1]
password = sys.argv[2]
# print sys.argv[1]
# print sys.argv[2]

# Authenticate to obtain SID
auth_url = 'https://www.google.com/accounts/ClientLogin'
auth_req_data = urllib.urlencode({'Email': username,
                                  'Passwd': password,
                                  'service': 'reader'})
auth_req = urllib2.Request(auth_url, data=auth_req_data)
auth_resp = urllib2.urlopen(auth_req)
auth_resp_content = auth_resp.read()
auth_resp_dict = dict(x.split('=') for x in auth_resp_content.split('\n') if x)
auth_token = auth_resp_dict["Auth"]

# Create a cookie in the header using the SID 
header = {}
header['Authorization'] = 'GoogleLogin auth=%s' % auth_token

reader_base_url = 'http://www.google.com/reader/api/0/unread-count?%s'
reader_req_data = urllib.urlencode({'all': 'true',
                                    'output': 'xml'})
reader_url = reader_base_url % (reader_req_data)
reader_req = urllib2.Request(reader_url, None, header)
reader_resp = urllib2.urlopen(reader_req)
reader_resp_content = reader_resp.read()

resultado = reader_resp_content

res2 = resultado.split(">")
final = []
for la in res2:
	final.extend(la.split("<"))
	
retorno = []
for la in final:
	if "http" in la:
		retorno.append(la)
retorno2 = []
for la in retorno:
	retorno2.append(la[5:])
if retorno2.count>0:
	print retorno2
	sys.exit(0)
else:
	sys.exit(1)
